package com.neusmart.tongrentang.wxapi;

import com.neusmart.tongrentang.R;
import com.neusmart.tongrentang.constants.Action;
import com.neusmart.tongrentang.constants.Key;
import com.neusmart.tongrentang.dataLoadRequest.DataLoadActivity;
import com.neusmart.tongrentang.model.Constants;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class WXPayEntryActivity extends DataLoadActivity implements IWXAPIEventHandler{
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       /* setContentView(R.layout.pay_result);
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);*/
    }

	@Override
	protected int getLayoutResID() {
		return R.layout.pay_result;
	}

	@Override
	protected void init() {
		api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
		api.handleIntent(getIntent(), this);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		// 判断返回错误码，如果支付成功则去后台查询支付结果再展示用户实际支付结果
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			/*AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.app_tip);*/
			//builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
			Intent intent = new Intent(Action.WECHAT_PAY);
			intent.putExtra(Key.WECHAT_PAY_RESULT, resp.errCode);
			sendBroadcast(intent);
			finish();
		}
	}
}